package com.sporadic.client.controller;

import com.alibaba.fastjson.JSON;
import com.sporadic.client.pojo.*;
import com.sporadic.client.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.OrderId;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin(origins = {"http://localhost:8080"}, methods = {RequestMethod.GET, RequestMethod.POST}, allowCredentials = "true")
public class Usercontroller {

    @Autowired
    private DishesService dishesService;
    @Autowired
    private DishesTypeService dishesTypeService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderdetailsService orderdetailsService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private DishesComboService DishesComboService;
    @Autowired
    private UserService userService;

//    查询全部菜品类型
    @RequestMapping("/selDishesAll")
    public List<Dishes> selDishesAll(Integer dishesMerchant){
        dishesMerchant = 1;//商家ID
        List<Dishes> list =  dishesService.selDishesAll(dishesMerchant);
        return list;
    }

    //    查询全部菜品类型和菜品的集合
    @RequestMapping("/findDshesTypeAll")
    public List<DishesType> findDshesTypeAll(@Param("dishesMerchant") Integer dishesMerchant){
        dishesMerchant = 1;//商家ID
        List<DishesType> list = dishesTypeService.findDshesTypeAll(dishesMerchant);
        return list;
    }

//    按照ID查询全部菜品
    @RequestMapping("/selDisheTypeAll")
    public List<DishesType> selDisheTypeAll(Integer dtMerchantId){
        dtMerchantId = 1;//商家ID
        List<DishesType> list = dishesTypeService.selDshesTypeAll(dtMerchantId);
        return list;
    }

//    根据菜品名称模糊查询
    @RequestMapping("/selDIshesLikeNameAll")
    public List<Dishes> selDIshesLikeNameAll(@Param("dishesName")String dishesName){
        List<Dishes> list = dishesService.selDishesLikeNameAll(dishesName);
        return list;
    }

    //    根据商家ID查询套餐信息
    @RequestMapping("/selDishesComboAll")
    public List<DishesCombo> selDishesComboAll(Integer dcMerchant){
        System.out.println("商家套餐查询");
        dcMerchant = 1;//商家ID
        List<DishesCombo> list = DishesComboService.selDishesComboAll(dcMerchant);
        return list;
    }

//    根据类型ID查询菜品分类
    @RequestMapping("/selDishesTypeIdAll")
    public List<Dishes> selDishesTypeIdAll(@RequestParam("dishesType")Integer disheType){
        System.out.println(disheType);
        List<Dishes> list = dishesService.selDishesTypeIdAll(disheType);
        return list;
    }

//    双向联动查询菜品信息
    @RequestMapping("/selDishesTypeDtNameAll")
    public List<Dishes> selDishesTypeDtNameAll(@RequestParam("dishesMerchant") Integer dishesMerchant){
        dishesMerchant = 1;
        List<Dishes> list = dishesService.selDishesTypeDtNameAll(dishesMerchant);
        return list;
    }

//    根据商家ID查询所有订单
    @RequestMapping("/selOrderAll")
    public List<Order> selOrderAll(@Param("orderMerchantId")Integer orderMerchantId,@Param("orderUserId")Integer orderUserId){
        orderMerchantId = 1;//商家ID
        orderUserId =1;//订单对应用户ID
        List<Order> list = orderService.selOrderAll(orderMerchantId,orderUserId);
        return list;
    }

//    根据ID查询订单编号
    @RequestMapping("/selOrderIdAll")
    public List<Order> selOrderIdAll(@Param("orderId") String orderId){
        List<Order> list = orderService.selOrderIdAll(orderId);
        return list;
    }

//    根据日期查询订单信息
    @RequestMapping("/selOrderCreateTimeAll")
    public List<Order> selOrderCreateTimeAll(@Param("createTime") Integer createTime){
        System.out.println(createTime);
        List<Order> list = null;
        if(createTime.equals(1)){
            list = orderService.selDataDayAll();
        }
        if(createTime.equals(2)){
            list = orderService.selDataWeekAll();
        }
        if(createTime.equals(3)){
            list = orderService.selDataMonthAll();
        }
        return list;
    }

//    根据订单编号查询所有订单详情
    @RequestMapping("/selOrderdetailsAll")
    public List<OrderDetails> selOrderdetailsAll(@Param("odOrderId")String odOrderId){
        List<OrderDetails> list = orderdetailsService.selOrderDetailsAll(odOrderId);
        return list;
    }

//    根据商家ID查询优惠券
    @RequestMapping("/selCouponAll")
    public List<Coupon> selCouponAll(Integer couponMerchant){
        couponMerchant = 1;
        List<Coupon> list = couponService.selCouponAll(couponMerchant);
        return list;
    }

//    查询用户是否注册
    @RequestMapping("/userSessionKey")
    public User userSessionKey(@Param("userSessionKey")String userSessionKey){
        System.out.println("进入sessionkey查询");
        User a = userService.UserSessionKey(userSessionKey);
        return a;
    }

//    用户注册信息
    @RequestMapping("/insUser")
    public int insUser(User user){
        User u1 = new User();
        u1.setUserName(user.getUserName());
        u1.setUserAddress(user.getUserAddress());
        u1.setUserHeadPortrait(user.getUserHeadPortrait());
        u1.setUserCreateTime(new Date());
        u1.setUserIntegral(0);
        u1.setUserPhone(user.getUserPhone());
        u1.setUserRankId(1);
        u1.setUserSessionKey(user.getUserSessionKey());
        int a = userService.insUser(u1);
        return u1.getUserId();
    }

//    绑定用户所属商家和桌号
    @RequestMapping("/updUser")
    public int updUser(@Param("userMerchant")Integer userMerchant ){
        User u1 = new User();
        u1.setUserMerchant(userMerchant);
        int a = userService.updUser(u1);
        return a;
    }

//    添加订单
    @RequestMapping("/insOrder")
    public int insOrder(Order order){
        Order o1 = new Order();
        o1.setOrderId(OrderId.createOrderId());
        int a = orderService.insOrder(order);
        return a;
    }

//    更新订单支付状态
    @RequestMapping("/updOrder")
    public int updOrder(Order order){
        Order o1 = new Order();
        int a = orderService.updOrder(order);
        return a;
    }

}
